package com.itheima.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.CheckGroupDao;
import com.itheima.dao.CheckItemDao;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Autowired
    private CheckItemDao checkItemDao;

    @Autowired
    private JedisPool jedisPool;

    @Override
    public void add(Integer[] checkgroupIds, Setmeal setmeal) {
        setmealDao.add(setmeal);
        Integer id = setmeal.getId();
        this.setSetmealAndCheckgroup(id, checkgroupIds);
        //将图片保存到集合中
        String img = setmeal.getImg();
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, img);
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<Setmeal> byCondition = setmealDao.findByCondition(queryString);
        PageResult pageResult = new PageResult();
        long total = byCondition.getTotal();
        List<Setmeal> result = byCondition.getResult();
        pageResult.setTotal(total);
        pageResult.setRows(result);
        return pageResult;
    }

    @Override
    public void delete(Integer id) {
        setmealDao.deleteBygroup(id);
        setmealDao.delete(id);
    }

    @Override
    public Setmeal findById(Integer id) {
        Setmeal setmeal = setmealDao.findById(id);
        System.out.println(setmeal);
        return setmeal;
    }

    public void setSetmealAndCheckgroup(Integer id, Integer[] checkgroupIds) {
        if (checkgroupIds != null && checkgroupIds.length > 0) {
            for (Integer checkgroupId : checkgroupIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("setmealId", id);
                map.put("checkgroupIds", checkgroupId);
                setmealDao.setSetmealAndCheckgroup(map);
            }
        }
    }

    @Override
    public List<Setmeal> findAll() {
        List<Setmeal> list = setmealDao.findAll();
        return list;
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return setmealDao.findCheckItemIdsByCheckGroupId(id);

    }

    @Override
    public void edit(Integer[] checkgroupIds, Setmeal setmeal) {
        //根据检查组id删除中间表数据(清理原有关联关系)
        Integer id = setmeal.getId();
        setmealDao.deleteAssociation(id);
        if (checkgroupIds != null & checkgroupIds.length > 0) {
            Map<String, Integer> map = new HashMap<>();
            for (Integer checkgroupId : checkgroupIds) {
                map.put("setmeal_id", id);
                map.put("checkgroup_id", checkgroupId);
                setmealDao.setCheckGroupAndCheckItem(map);
            }
        }
        setmealDao.edit(setmeal);
    }

    @Override
    public List<Setmeal> getAllSetmeal() {
        return setmealDao.getAllSetmeal();
    }

    @Override
    public Setmeal findByIdSetmeal(Integer id) {
        Setmeal setmeal = setmealDao.findByIdSetmeal(id);
        //这个是通过setmeal的id先去查询setmeal_checkGroup这个表.查询到setmeal这个id相对应的checkgroupd的id
        //再根据checkgroupd的id去查询checkgroupd这个表的所有,返回的是List<CheckGroup> list
        List<CheckGroup> list = checkGroupDao.listBySetmealId(id);
        if (list != null && list.size() > 0) {
            for (CheckGroup checkGroup : list) {
                System.out.println(checkGroup+"===");
                //我们通过遍历checkgroup拿到每一个的checkgroup的id,这个时候在根据checkgroup_checkitem这个表查询到
                //checkgroup的id相对于的checkitem的id,再根据checkitem的id去查询到所有的checkitemn,返回的是List<CheckItem>list1
                Integer checkgroupid = checkGroup.getId();
                List<CheckItem>list1=checkItemDao.findCheckItemByCheckGroupId(checkgroupid);
                checkGroup.setCheckItems(list1);
            }
            setmeal.setCheckGroups(list);
        }
        return setmeal;
    }
}
